package example.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import example.springboot.dto.PaginationDTO;
import example.springboot.mapper.listMapper;
import example.springboot.mapper.questionListMapper;
import example.springboot.mapper.userMapper;
import example.springboot.model.Question;
import example.springboot.model.t_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.security.PrivateKey;
import java.util.List;

/**
 * 首页问题显示
 */
@Controller
public class listController {
    @Autowired
    private listMapper listMapper;
    @Autowired
    example.springboot.mapper.questionListMapper questionListMapper;
    @Autowired
    userMapper userMapper;
    /**
     *  问题页面展示
     * @return
     */
    @RequestMapping("/")
    public String questiondisplay(Model model,
                                  @RequestParam(name = "page", defaultValue = "1") Integer page,
                                  HttpServletRequest request){
        Integer limit;
        List<Question> questions = questionListMapper.selectAll();
        PaginationDTO<Question> paginationDTO=new PaginationDTO();
        limit=questions.size()/5;
        if (questions.size()%5!=0) limit++;
        paginationDTO.setTotalPage(limit);//设置页数
        paginationDTO.setPagination(limit,page);
        //获取每页的问题
        List<Question> listquestion=questionListMapper.questionlist((paginationDTO.getPage()-1)*5,5);
        for (Question question:listquestion){
            userMapper.byAccountidFind(question.getCreator());
            question.setUser(userMapper.byAccountidFind(question.getCreator()));
        }
        paginationDTO.setData(listquestion);
        model.addAttribute("pagination",paginationDTO);

        return "list";
    }
}
